package com.lm.scau.dyb.utils;

import android.util.ArrayMap;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.lm.scau.dyb.activity.base.BaseActivity;
import com.lm.scau.dyb.bean.PackageBean;
import com.lm.scau.dyb.inter.HttpExecuteEvent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


/**
 * Function:
 * auth：liming on 2016/8/12 15:19
 * mail：liming@wxchina.com
 */
public class HttpUtils {
    private BaseActivity activity;

    public HttpUtils(BaseActivity activity) {
        this.activity = activity;
    }

    /**
     * 发送http get请求，请求字符串信息，然后再到本地处理
     *
     * @param url          请求服务器地址，按照规定的协议
     * @param executeEvent 处理的接口
     */
    public void httpRequestGet(String url, final boolean isShowLoading, final HttpExecuteEvent executeEvent) {
        if (isShowLoading) {
            activity.loading();
        }
        JSONObject obj = null;
        try {
            obj = new JSONObject(url);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, FlagHelper.SERVER_URL,
                obj, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject result) {
                if (isShowLoading) {
                    activity.dismiss();
                }
                try {
                    if (FlagHelper.RSP_CODE == result.getJSONObject("result").getInt(FlagHelper.RSPCODE)) {
                        executeEvent.onResponeSuccess(result.toString());
                    } else {
                        executeEvent.onResponeError(result.toString());
                    }
                } catch (Exception e) {
                    executeEvent.onResponeError(e.getMessage());
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (isShowLoading) {
                    activity.dismiss();
                }
                error.printStackTrace();
                executeEvent.onResponeError(error.getMessage());
            }
        });
        activity.getmQueue().add(request);
    }

}
